<template>
  <div>
    <tableComponents
      :pagination="{ currentPage, pageSize, total: list.all_num }"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="getSearch"
      :searchData="userSearchData"
      :isHaveExport="false"
      @toAdd="editBtn()"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          stripe
          v-loading="loading"
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
        >
          <el-table-column label="排序" width="90">
            <template slot-scope="scope">
              <el-input
                @change="sortBtn(scope.row)"
                v-model.number="scope.row.sort"
                size="small"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="图片" min-width="100" align="center">
            <template slot-scope="scope">
              <el-popover placement="right" title trigger="hover">
                <img :src="scope.row.piclink" style="width: 300px" />
                <img
                  slot="reference"
                  :src="scope.row.piclink"
                  :alt="scope.row.piclink"
                  style="width: 80px"
                />
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="140">
            <template slot-scope="scope">
              {{ scope.row.title || "----" }}
            </template>
          </el-table-column>

          <el-table-column label="价格" min-width="120">
            <template slot-scope="scope">
              {{ scope.row.price | keepTwoNum }}
            </template>
          </el-table-column>

          <el-table-column label="获得油卡数量" min-width="120">
            <template slot-scope="scope">
              {{ scope.row.oil_card | keepTwoNum }}
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="100">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                type="primary"
                size="mini"
                @click="editBtn(scope.row)"
              >
                编辑
              </el-button>
              <el-button
                :loading="$store.state.isLoading"
                type="danger"
                size="mini"
                @click="delBtn(scope.row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      :title="editInfo.id ? '编辑' : '添加'"
      :modal-append-to-body="false"
      :visible.sync="show_edit"
      width="800px"
      class="tl size_13"
    >
      <el-form :label-width="formLabelWidth" class="demo-ruleForm">
        <el-form-item label="标题">
          <el-col :span="12">
            <el-input v-model="editInfo.title" size="small"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="价格">
          <el-col :span="12">
            <el-input v-model="editInfo.price" size="small"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="获得油卡数量">
          <el-col :span="12">
            <el-input v-model="editInfo.oil_card" size="small"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="图片地址">
          <el-col :span="12" class="tl">
            <el-input
              v-model="editInfo.piclink"
              clearable
              class="mar_bottom_5" disabled
            ></el-input>

            <uploadpic
              :piclink="editInfo.piclink"
              :width="150"
              :height="150"
              :real_width="50"
              :real_height="50"
              :is_more="false"
              @send_pic="onSuccess"
            >
            </uploadpic>
          </el-col>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="subBtn">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import uploadpic from "@/components/page/pic/up_pic";
import { oilCardSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      userSearchData: oilCardSearchData,
      loading: false,
      list: [],
      pageSize: 10,
      currentPage: 1,
      searchAll: {},

      formLabelWidth: "120px",
      editInfo: {},
      show_edit: false,
    };
  },
  components: {
    uploadpic,
  },
  created() {
    this.getNewsAll();
  },
  methods: {
    sortBtn(row) {
      this.$api.getStation.oil_card_saveedit(row).then((res) => {
        if (res.data.code == 200) {
          this.getNewsAll();
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    delBtn(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          var data = { id_str: id };
          this.$api.getStation.oil_card_del(data).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },
    onSuccess(pic) {
      this.editInfo.piclink = pic;
    },
    editBtn(item) {
      if (item) {
        this.editInfo = item;
        this.editInfo.price = this.$fnc.toFixedZ(this.editInfo.price);
        this.editInfo.oil_card = this.$fnc.toFixedZ(this.editInfo.oil_card);
      } else {
        this.editInfo = {
          title: "", //标题
          price: "", //价格
          oil_card: "", //获得的油卡数量
          piclink: "", //图片
        };
      }
      this.show_edit = true;
    },
    subBtn() {
      if (this.editInfo.id) {
        this.$api.getStation.oil_card_saveedit(this.editInfo).then((res) => {
          if (res.data.code == 200) {
            this.$fnc.alertSuccess({ text: "编辑成功" });
            this.getNewsAll();
            this.show_edit = false;
          } else {
            this.$fnc.alertError(res.data.code_desc, res.data.result);
          }
        });
      } else {
        this.$api.getStation.oil_card_saveadd(this.editInfo).then((res) => {
          if (res.data.code == 200) {
            this.$fnc.alertSuccess({ text: "添加成功" });
            this.getNewsAll(this.searchAll);
            this.show_edit = false;
          } else {
            this.$fnc.alertError(res.data.code_desc, res.data.result);
          }
        });
      }
    },
    getSearch(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;

      this.$api.getStation.oil_card_lists(params).then((res) => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
  },
};
</script>

<style lang="less" scoped>
</style>
